'use strict';

angular.module('app')
    .controller('roleController', [ '$rootScope', '$scope', '$http', '$state',
        function($rootScope, $scope, $http, $state) {
            $scope.title = '角色管理';
            $scope.param = { };
            $scope.loading = false;

            $scope.search = function () {
                $scope.loading = true;
                if($scope.pageFlag != 1){//未点击翻页
                    $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
                }else{//点击翻页
                    $scope.pageFlag = 2;//将开关值设置为2
                }
                $.ajax({
                    url : '/role/read/list',
                    type: 'PUT',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    data: angular.toJson($scope.param)
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.search();

            //从本地获取localstorage数据，对权限按钮做隐藏
            var personAuth =localStorage.getItem("PersonAuth");//获取PersonAuth的值
            personAuth = personAuth.split(',');
            for(var i = 0;i<personAuth.length;i++){
                if(personAuth[i]=='sys.base.role.read'){
                    $scope.read = true;
                }
                if(personAuth[i]=='sys.base.role.add'){
                    $scope.add = true;
                }
                if(personAuth[i]=='sys.base.role.update'){
                    $scope.update = true;
                }
                if(personAuth[i]=='sys.base.role.add.delete'){
                    $scope.dele = true;
                }
            }

            $scope.clearSearch = function() {
                $scope.param.keyword= null;
                $scope.search();
            }

            $scope.disableItem = function(id, enable) {
                $.ajax({
                    url : '/role/disableRole',
                    type: 'PUT',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    data: angular.toJson({'roleId': id})
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result;
                        $scope.search();
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });


            }
            $scope.enableItem = function(id, enable) {
                $.ajax({
                    url : '/role/enableRole',
                    type: 'PUT',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    data: angular.toJson({'roleId': id})
                }).then(function(result) {
                    $scope.loading = false;
                    console.log(result.httpCode,result);
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result;
                        $scope.search();
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });


            }

            // 翻页
            $scope.pagination = function (page) {
                $scope.param.pageNum=page;
                $scope.pageFlag = 1;//将开关值设置为1
                $scope.search();
            };
        } ]);